package com.e3.sysmg.security.client;

import java.util.ArrayList;
import java.util.List;

import com.e3.sysmg.security.model.UserModel;
import com.e3.web.core.client.GridListener;
import com.e3.web.core.client.ListenerManager;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridSelectionModel;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.Element;

public class UserGridPanel extends LayoutContainer {
	
	private Grid<UserModel> userGrid;
	private ListStore<UserModel> gridStore;
	private GridSelectionModel<UserModel> gridSelectionModel;
	
	private ListenerManager<UserModel> listenerManager = new ListenerManager<UserModel>();
	
	@Override
	protected void onRender(Element parent, int index) {
		super.onRender(parent, index);
		setLayout(new FitLayout());
		
		add(getUserGrid());
	}
	
	public void addGridListener(GridListener<UserModel> listener){
		listenerManager.addGridListener(listener);
	}
	
	public Grid<UserModel> getUserGrid(){
		if(userGrid == null){
			List<ColumnConfig> configs = UserHelper.INSTANCE.getColumnConfigs();
			ColumnModel cm = new ColumnModel(configs);
			userGrid = new Grid<UserModel>(getGridStore(),cm){
				protected void onDoubleClick(GridEvent<UserModel> e) {
				    onGridRowDoubleClicked(e.getModel());
				}
			};
			userGrid.setSelectionModel(getGridSelectionModel());
		}
		return userGrid;
	}
	
	public ListStore<UserModel> getGridStore(){
		if(gridStore == null){
			gridStore = new ListStore<UserModel>();
			gridStore.setModelComparer(UserHelper.INSTANCE.getModelComparer());
			gridStore.setKeyProvider(UserHelper.INSTANCE.getModelKeyProvider());
		}
		return gridStore;
	}
	
	private GridSelectionModel<UserModel> getGridSelectionModel(){
		if(gridSelectionModel==null){
			gridSelectionModel = new GridSelectionModel<UserModel>();
			gridSelectionModel.addSelectionChangedListener(new SelectionChangedListener<UserModel>(){

				@Override
				public void selectionChanged(SelectionChangedEvent<UserModel> se) {
					UserModel model = se.getSelectedItem();
					onGridSelectionChanged(model);
				}
			});
			
		}
		return gridSelectionModel;
	}
	
	private void onGridSelectionChanged(UserModel model) {
		listenerManager.notifyGridSelectChanged(model);
	}
	
	private void onGridRowDoubleClicked(UserModel model){
		listenerManager.notifyGridRowDoubleClicked(model);
	}
	
	public List<UserModel> getSelectedUsers(){
		return getGridSelectionModel().getSelectedItems();
	}
	
	public void removeDatas(List<UserModel> models){
		if(models == null || models.isEmpty()){
			return;
		}
		for (UserModel roleModel : models) {
			getGridStore().remove(roleModel);
		}
	}
	
	public void removeAllData(){
		getGridStore().removeAll();
	}
	
	public void addGridValues(List<UserModel> models){
		if(models == null || models.isEmpty()){
			return;
		}
		getGridStore().add(models);
	}
	
	public void setGridValues(List<UserModel> models){
		removeAllData();
		if(models!=null&&models.size()>0){
			getGridStore().add(models);
			List<UserModel> ums = new ArrayList<UserModel>(1);
			ums.add(models.get(0));
			getGridSelectionModel().setSelection(ums);
		}
	}
	
	public void setSelectValue(UserModel model){
		if(model==null){
			return;
		}
		List<UserModel> models = new ArrayList<UserModel>(1);
		models.add(model);
		getGridSelectionModel().setSelection(models);
	}

	
	public void refreshGridData(UserModel model){
		if(model==null){
			return;
		}
		List<UserModel> models = getGridStore().getModels();
		boolean isUpdate = false;
		if(models!=null&&models.size()>0){
			for (UserModel roleModel : models) {
				if(roleModel.getId().equals(model.getId())){
					isUpdate = true;
					break;
				}
			}
			if(isUpdate){
				getGridStore().update(model);
				setSelectValue(model);
				return;
			}
		}
		List<UserModel> roles = new ArrayList<UserModel>(1);
		roles.add(model);
		getGridStore().add(roles);
		setSelectValue(model);
	}

}
